<template>
  <div class="app-container">
      方舱名称：  <el-input
    style="width: 200px"
    placeholder="请输入内容"
    prefix-icon="el-icon-search"
    v-model="sheltersName">
  </el-input>
    <el-button type="primary" style="margin: 20px" @click="selectShelters()">搜索</el-button>
    <el-button type="primary" style="margin: 20px" @click="resetForm()">重置</el-button>
    <el-button type="primary" @click="dialogFormVisible = true">添加</el-button>


    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
      type="selection"
      width="55">
      </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="方舱名称" width=auto>
        <template slot-scope="scope">
          {{ scope.row.sheltersName }}
        </template>
      </el-table-column>
      <el-table-column label="联系方式"
                       width="110"
                       align="center">
        <template slot-scope="scope">
          {{ scope.row.sheltersPhone }}
        </template>
      </el-table-column>
      <el-table-column label="方舱地址" width=auto align="center">
        <template slot-scope="scope">
          {{ scope.row.sheltersAddress }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" width=auto align="center">
        <template slot-scope="scope">
          <el-button type="success" plain @click="editor(scope.row)">编辑</el-button>
          <el-button type="danger" plain @click="deleteShelters(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <el-dialog title="添加方舱"
               :visible.sync="dialogFormVisible">
      <el-form :model="shelters">
        <el-form-item label="方舱名称"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.sheltersName"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="方舱联系方式"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.sheltersPhone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="方舱地址"
                      :label-width="formLabelWidth">
          <el-input v-model="shelters.sheltersAddress"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="insertShelters()">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="修改区审核员信息"
               :visible.sync="dialogForm1Visible">
      <el-form :model="shelters1">
        <el-form-item label="方舱名称"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.sheltersName"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="方舱联系方式"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.sheltersPhone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="方舱地址"
                      :label-width="form1LabelWidth">
          <el-input v-model="shelters1.sheltersAddress"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogForm1Visible = false">取 消</el-button>
        <el-button type="primary"
                   @click="updateShelters()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页插件 -->
    <div class="block" style="float:right">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pageIndex"
      :page-sizes="pageSizes"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Total">
    </el-pagination>
    </div>
  </div>
</template>

<script>
// import { deleteShelters,updateShelters } from '@/api/preAdmin/centerAdmin'
import { selectShelters,insertShelters,deleteShelters,updateShelters } from '@/api/preRegion/preRegion'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      sheltersName: null,
      sheltersAccount: null,
      sheltersId: null,
      sheltersId1: "",
      sheltersId2: null,
      begin: null,
      end: null,
      //分页
      currentPage:1,
      Total:0,
      pageIndex:1,
      pageSize:5,
      pageSizes: [5, 10, 15, 20],
      // sheltersIdList:"",
      newName:"",
      dialogFormVisible: false,
      dialogForm1Visible: false,
      shelters: {
        // userName: '',
        // areaId: '',
        // userPhone: '',
        // account: '',
        // userPassword: ''
        sheltersName: "",
        sheltersPhone: "",
        sheltersAddress: "",
        sheltersStatus: "1"
      },
      shelters1: {
        sheltersId: "",
        sheltersName: "",
        sheltersPhone: "",
        sheltersAddress: "",
        sheltersStatus: "1"
      },
      formLabelWidth: '120px',
      form1LabelWidth: '120px'
    }
  },
  created() {
    this.fetchData()
    // this.getRegionList()
  },
  methods: {
    fetchData() {
      // console.log(1)
      this.listLoading = true
      selectShelters(this.pageIndex,this.pageSize).then(response =>{
        this.list = response.obj.list
        localStorage.setItem("list",JSON.stringify(this.list))
        this.Total = response.obj.total
        this.listLoading = false
      })
      // console.log(1)
    },
    // 查询管理员
    selectShelters(){
      if(this.sheltersName == ""){
        alert("查询条件不能为空！")
      }else{
        selectShelters(this.pageIndex,this.pageSize,this.sheltersName).then(response =>{
        this.list = response.obj.list
        this.Total = response.obj.total
        this.listLoading = false
      })
      }
    },
    //重置查询条件
     resetForm(){
      this.sheltersName = null
      this.fetchData()
      // this.list = JSON.parse(localStorage.getItem("list"))
    },
    //增加管理员
      insertShelters(){
        insertShelters(this.shelters).then(response => {
          let message = response.msg
          this.$message({
              type: 'success',
              message: message
          })
          this.dialogFormVisible = false
          this.fetchData()      
          this.shelters.sheltersName = "",
          this.shelters.sheltersPhone = "",
          this.shelters.sheltersAddress = ""
      })
      },
    //编辑管理员
      editor(item){
        this.dialogForm1Visible = true
        // this.sheltersId1 = item.shelters.sheltersName
        this.shelters1 = item
        // console.log(item)
      },
      updateShelters(){
        // this.sheltersIdList = this.sheltersId1
        // console.log(this.sheltersId1)
        // console.log(this.sheltersIdList)
        updateShelters(this.shelters1).then(response =>{
          // console.log(this.areaIdList)
          let message = response.msg
           this.$message({
              type: 'success',
              message: message
              })
        this.dialogForm1Visible = false
        this.fetchData()
        })
      },
      //删除管理员
      deleteShelters(item){
        console.log(item)
        deleteShelters(item.sheltersId).then(response =>{
          this.$message({
            message:response.msg,
            type:'success'
          })
          this.fetchData()
        })
      },
      // 分页
         // 每页显示的条数
        handleSizeChange(val) {
            // 改变每页显示的条数 
            this.pageSize=val
            // this.pageSize=val
            // 点击每页显示的条数时，显示第一页
            // this.fetchData.getRegionCheck(1,val,1)
            // 在改变每页显示的条数时，要将页码显示到第一页
            // this.pageIndex=1
            this.fetchData()
        },
          // 显示第几页
        handleCurrentChange(val) {
            // 改变默认的页数
            this.pageIndex=val
            // 切换页码时，要获取每页显示的条数
            // this.pageSize=val
            // this.pageSize = (val)*(this.pageSize)
            this.fetchData()
        }
  }
}
</script>
